package migrate

import (
	"database/sql"
	"gorm.io/gorm"
)

// migrationFunc 定义 up 和 down 回调方法的类型
type migrateFunc func(migrator gorm.Migrator, db *sql.DB)

// MigrationFile 代表着单个迁移文件
type MigrationFile struct {
	Up       migrateFunc
	Down     migrateFunc
	FileName string
}

// migrationFiles 所有的迁移文件数组
var migrationFiles []MigrationFile

// Add 新增一个迁移文件，所有的迁移文件都需要调用此方法来注册
func Add(name string, up migrateFunc, down migrateFunc) {
	migrationFiles = append(migrationFiles, MigrationFile{
		Up:       up,
		Down:     down,
		FileName: name,
	})
}


// getMigrationFile 通过迁移文件的名称来获取到 MigrationFile 对象
func getMigrationFile(name string) MigrationFile {
	for _, item := range migrationFiles {
		if item.FileName == name {
			return item
		}
	}
	return MigrationFile{}
}

//isNotMigrated  判断迁移是否已执行
func (f MigrationFile) isNotMigrated(migrations []Migration) bool {
	for _, migration := range migrations {
		if f.FileName == migration.Migration {
			return false
		}
	}
	return true
}
